package com.zego.wawaji.ui.rank.order;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;

import com.zego.wawaji.R;
import com.zego.wawaji.base.module.ModuleMgr;
import com.zego.wawaji.base.observer.MsgMgr;
import com.zego.wawaji.base.observer.MsgType;
import com.zego.wawaji.base.observer.PObserver;
import com.zego.wawaji.http.request.HttpResponse;
import com.zego.wawaji.http.request.RequestComplete;
import com.zego.wawaji.http.request.UrlParam;
import com.zego.wawaji.ui.base.BaseAppCompatActivity;
import com.zego.wawaji.ui.base.CustomRecyclerView;
import com.zego.wawaji.ui.base.xlistview.ExListView;
import com.zego.wawaji.ui.rank.bean.Award;
import com.zego.wawaji.ui.rank.bean.AwardList;

import java.util.ArrayList;
import java.util.List;

/**
 * 排行榜： 奖品订单
 * Created by Su on 2018/1/16.
 */
public class PrizeOrderActivity extends BaseAppCompatActivity implements PObserver, RequestComplete, ExListView.IXListViewListener, View.OnClickListener {
    private CustomRecyclerView statusLayout;
    private ExListView exListView;
    private int page = 1; // 加载页码

    private PrizeOrderAdapter adapter;
    private List<Award> list = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.prize_order_activity);
        initView();
        onRefresh();
    }

    private void initView() {
        setBackView();
        setTitle("我的订单");

        MsgMgr.getInstance().attach(this);
        statusLayout = (CustomRecyclerView) findViewById(R.id.custom_layout);
        View mViewTop = LayoutInflater.from(this).inflate(R.layout.layout_margintop, null);
        exListView = statusLayout.getExListView();
        exListView.setXListViewListener(this);
        exListView.setPullRefreshEnable(true);
        exListView.setPullLoadEnable(true);
        exListView.addHeaderView(mViewTop);
        adapter = new PrizeOrderAdapter(this, list);
        exListView.setAdapter(adapter);
        statusLayout.showLoading();
    }

    @Override
    public void onRefresh() {
        exListView.setPullRefreshEnable(true);
        exListView.setPullLoadEnable(true);
        page = 1;
        ModuleMgr.getWawaMgr().reqAwardOrderList(page, this);
    }

    @Override
    public void onLoadMore() {
        page++;
        ModuleMgr.getWawaMgr().reqAwardOrderList(page, this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

        }
    }

    @Override
    public void onRequestComplete(HttpResponse response) {
        exListView.stopRefresh();
        exListView.stopLoadMore();
        if (response.getUrlParam() == UrlParam.reqAwardList) {
            setData(response);
        }
    }

    private void setData(HttpResponse response) {
        // 请求失败
        if (!response.isOk()) {
            if (list.size() != 0) {
                if (list.size() < 10) {
                    exListView.setPullLoadEnable(false);
                }
                adapter.notifyDataSetChanged();
                statusLayout.showExListView();
                return;
            }
            statusLayout.showNoData();
            return;
        }

        // 非缓存
        if (!response.isCache()) {
            AwardList awardList = (AwardList) response.getBaseData();

            if (awardList.getAwardList().size() != 0) {
                //如果请求的第一页 应该把之前的数据都清掉，把返回的数据作为第一页
                if (page == 1) {
                    if (list.size() != 0) {
                        list.clear();
                    }
                    page = 1;
                }

                list.addAll(awardList.getAwardList());
                if (list.size() < 10) {
                    exListView.setPullLoadEnable(false);
                }
                adapter.notifyDataSetChanged();
                statusLayout.showExListView();
            } else {
                if (page == 1) {
                    statusLayout.showNoData();
                } else {
                    exListView.setPullLoadEnable(false);
                }
            }
            return;
        }

        // 缓存
        if (page == 1) {
            AwardList awardList = (AwardList) response.getBaseData();

            if (awardList != null && awardList.getAwardList().size() != 0) {
                if (list.size() != 0) {
                    list.clear();
                }
                list.addAll(awardList.getAwardList());
                if (list.size() < 10) {
                    exListView.setPullLoadEnable(false);
                }
                adapter.notifyDataSetChanged();
                statusLayout.showExListView();
            }
        }
    }

    @Override
    public void finish() {
        super.finish();
        MsgMgr.getInstance().detach(this);
    }

    @Override
    public void onMessage(String key, Object value) {
        switch (key) {
            case MsgType.MT_Prize_Order_Refresh:
                onRefresh();
                break;
        }
    }
}